// 歌手页面
<template>
  <div class="singer">
    <list-view @select="selectSinger" :data="singers"></list-view>
    <router-view></router-view>
  </div>
</template>
<script type="text/ecmascript-6">
import { getSingerList } from 'api/singer'; //获取歌手数据api接口
import { ConvertPinyin } from 'common/js/convertPinyin'; //获取汉字或英文首字母函数
import { ERR_OK } from 'api/config'; //判断数据请求是否正确：0
import originJSONP from 'jsonp'; //引入jsonp原始插件
import ListView from 'base/listview/listview_2'; //歌手列表组件
import { mapMutations } from 'vuex'; //vuex提供的语法堂

const HOT_NAME = '热门';
const HOT_SINGER_LEN = 10;

export default {
  data() {
    return {
      singers: []
    }
  },
  components: {
    ListView
  },
  created() {
    this._getSingerList()
  },
  methods: {
    // 歌手点击事件
    selectSinger(singer) {
      // console.log(singer);
      this.$router.push({
        path: `/singer/${singer.id}`
      })
      this.setSinger(singer) //对vuex的mutations进行提交
    },
    // 获取歌手数据
    _getSingerList() {
      getSingerList().then((res) => {
        if (res.code === ERR_OK) {
          let list = [];
          res.singerList.data.singerlist.forEach((item, index) => {
            item['Findex'] = ConvertPinyin(item.singer_name);
            list.push(item);
          });
          // console.log(this._normalizeSinger(list)); 
          this.singers = this._normalizeSinger(list);
        }
      })

    },
    // 格式化歌手数据
    _normalizeSinger(list) {
      // 定义一个歌手对象
      let map = {
        hot: {
          title: HOT_NAME,
          items: []
        }
      }
      // 给map填充数据
      list.forEach((item, index) => {
        if (index < HOT_SINGER_LEN) {
          map.hot.items.push({
            id: item.singer_mid,
            name: item.singer_name,
            avatar: `https://y.gtimg.cn/music/photo_new/T001R150x150M000${item.singer_mid}.jpg?max_age=2592000`
          })
        }
        const key = item.Findex;
        if (!map[key]) {
          map[key] = {
            title: key,
            items: []
          }
        }
        map[key].items.push({
          id: item.singer_id,
          name: item.singer_name,
          avatar: `https://y.gtimg.cn/music/photo_new/T001R150x150M000${item.singer_mid}.jpg?max_age=2592000`
        })
      });
      // 处理map，得到有序列表
      let hot = []; //热门数组
      let ret = []; //其余数组
      for (let key in map) {
        let val = map[key];
        if (val.title.match(/[a-zA-Z]/)) {
          ret.push(val)
        } else if (val.title === HOT_NAME) {
          hot.push(val)
        }
      }
      ret.sort((a, b) => {
        return a.title.charCodeAt(0) - b.title.charCodeAt(0)
      })
      return hot.concat(ret)
    },
    // vuex：做一个对象的映射
    ...mapMutations({
      setSinger: 'SET_SINGER'
    })
  }
}
</script>
<style lang="stylus" scoped rel="stylesheet/stylus">
.singer
  position: fixed
  top: 88px
  bottom: 0
  width: 100%
</style>